/*
**        DroidPlugin Project
**
** Copyright(c) 2015 Andy Zhang <zhangyong232@gmail.com>
**
** This file is part of DroidPlugin.
**
** DroidPlugin is free software: you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation, either
** version 3 of the License, or (at your option) any later version.
**
** DroidPlugin is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with DroidPlugin.  If not, see <http://www.gnu.org/licenses/lgpl.txt>
**
**/

package gsw.tool.pluggable.hook.handler;

import android.app.Activity;
import android.app.Application;
import android.app.Instrumentation;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

/**
 * 自定义Activity监听器，用来扩展Activity的生命周期方法
 */
public class HandlerInstrumentation extends Instrumentation {

    protected static final String TAG = HandlerInstrumentation.class.getSimpleName();
    //系统的Instrumentation
    protected Instrumentation mTarget;
    //监听器是否可用，默认可用
    private boolean enable = true;

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public HandlerInstrumentation(Instrumentation target) {
        mTarget = target;
    }

    /**
     * 监听Application的onCreate()
     *
     * @param app 宿主的application
     */
    @Override
    public void callApplicationOnCreate(Application app) {
        Log.i(TAG, "callApplicationOnCreate: ");
    }

    /**
     * 监听Activity的OnCreate()
     *
     * @param activity 代理Activity
     * @param icicle   传递的Bundle对象
     */
    @Override
    public void callActivityOnCreate(Activity activity, Bundle icicle) {
        Log.i(TAG, "callActivityOnCreate: ");
        super.callActivityOnCreate(activity, icicle);
    }

    /**
     * 监听Activity的OnDestroy()
     *
     * @param activity 代理Activity
     */
    @Override
    public void callActivityOnDestroy(Activity activity) {
        Log.i(TAG, "callActivityOnDestroy: ");
        if (mTarget != null) {
            mTarget.callActivityOnDestroy(activity);
        } else {
            super.callActivityOnDestroy(activity);
        }
    }

    /**
     * 监听Activity的onNewIntent()
     *
     * @param activity 代理Activity
     * @param intent   传递的意图
     */
    @Override
    public void callActivityOnNewIntent(Activity activity, Intent intent) {
        Log.i(TAG, "callActivityOnNewIntent: ");
        super.callActivityOnNewIntent(activity, intent);
    }
}
